.PHONY : all clean cleandep

OBJS_PATH=obj
vpath %.o $(OBJS_PATH)
vpath %.d $(OBJS_PATH)
#vpath %.c ../../
#vpath %.cpp ../../

TARGET = language
all: $(TARGET)

GCC = gcc
GXX = g++
INCLUDES =-I$(CURDIR) -I../../ -I../../include
CFLAGS = -DLINUX -Wall -Werror -ggdb
CXXFLAGS = $(CFLAGS)
LDFLAGS = -L../../lib

LIBS=-lpcre -lcppstory

SOURCES = $(wildcard *.cpp)
SOURCES += $(wildcard features/*.cpp)
SOURCES += $(wildcard steps/*.cpp)
CSOURCES =

OBJS = $(patsubst %.cpp, $(OBJS_PATH)/%.o, $(SOURCES))
OBJS += $(patsubst %.c, $(OBJS_PATH)/%.o, $(CSOURCES))

DEPENDS = $(patsubst %.cpp, $(OBJS_PATH)/%.d, $(SOURCES))
DEPENDS += $(patsubst %.c, $(OBJS_PATH)/%.d, $(CSOURCES))
-include $(DEPENDS)

$(TARGET): $(OBJS)
	@[ ! -e $(dir $@) ] &  mkdir -p $(dir $@)
	$(GXX) -o $(TARGET) $(OBJS) $(LIBS) $(LDFLAGS)

$(OBJS_PATH)/%.o:%.cpp
	@[ ! -e $(dir $@) ] &  mkdir -p $(dir $@)
	$(GXX) $(INCLUDES) $(CXXFLAGS) -c $< -o $@

$(OBJS_PATH)/%.d:%.cpp
	@[ ! -e $(dir $@) ] &  mkdir -p $(dir $@)
	@$(GCC) $(INCLUDES) $(CXXFLAGS) -MM $< > $@.tmp
	@sed 's,\(.*\)\.o[ ]*:,$(dir $@)\1.o :,g' < $@.tmp > $@
	@#$(GXX) $(INCLUDES) $(CXXFLAGS) -MM -MD $< -o $@

$(OBJS_PATH)/%.o:%.c
	@[ ! -e $(dir $@) ] &  mkdir -p $(dir $@)
	$(GCC) $(INCLUDES) $(CFLAGS) -c $< -o $@

$(OBJS_PATH)/%.d:%.c
	@[ ! -e $(dir $@) ] &  mkdir -p $(dir $@)
	@$(GCC) $(INCLUDES) $(CFLAGS) -MM $< > $@.tmp
	@sed 's,\(.*\)\.o[ ]*:,$(dir $@)\1.o :,g' < $@.tmp > $@
	@#$(GCC) $(INCLUDES) $(CFLAGS) -MM -MD $< -o $@

clean: cleandep
	find $(OBJS_PATH) -type f | xargs rm -rf
	rm -rf $(OBJS) $(TARGET) $(TARGET).exe

cleandep:
	rm -rf $(DEPENDS)
